<?php
require_once("../../Config.php");
require_once("../../Class/ClassFunc.php");
require_once("../../Class/ClassMysql.php");
require_once("../../Class/ClassCheck.php");
require_once("../../Class/ClassResult.php");

//json头
header("Content-type: application/json");

session_start();
if(!isset($_SESSION["name"])) {
    exit(Result::jsonReturn(1, "未登录"));
}

$uid = $_SESSION["id"];
$uname = $_SESSION["name"];
$uemail = $_SESSION["email"];
$upower = $_SESSION["power"];


$info = array();
$id = 0;
if(!Check::isEmpty($_POST["id"])) {
    if(Check::isNum($_POST["id"]) && (int)$_POST["id"]>0) {
        $id = (int)$_POST["id"];
    }else {
        exit(Result::jsonReturn(1, "参数格式错误"));
    }
}else {
    exit(Result::jsonReturn(1, "参数不足"));
}
if(!Check::isEmpty($_POST["title"])) {
    if(Check::isName($_POST["title"])) {
        $info["title"] = $_POST["title"];
    }else {
        exit(Result::jsonReturn(1, "标题格式有误"));
    }    
}
if(!Check::isEmpty($_POST["slug"])) {
    if(Check::isSlug($_POST["slug"])) {
        $info["slug"] = $_POST["slug"];
    }else {
        exit(Result::jsonReturn(1, "缩略名格式错误"));
    }
}
if(!Check::isEmpty($_POST["type"])) {
    if(Check::isNum($_POST["type"]) && in_array((int)$_POST["type"], array(0, 1))) {
        $info["type"] = (int)$_POST["type"];
    }else {
        exit(Result::jsonReturn(1, "类型格式错误"));
    }
}
if(!Check::isEmpty($_POST["text"])) {
    $info["text"] = $_POST["text"];
}

$db = new DB();
if($_POST["checked"] == 'none') {
    $sql = "DELETE FROM qilian_relationships WHERE content_id={$id}";
    $db->delete($sql);
}elseif($_POST["checked"] != NULL) {
    foreach($_POST["checked"] as $value) {
        if(!Check::isNum($value) || (int)$value<=0) {
            exit(Result::jsonReturn(1, "选择列表参数不正确"));
        }
    }
    $info["belong"] = $_POST["checked"];
}
if(count($info) == 0) {
    exit(Result::jsonReturn(1, "无更新内容"));
}


if(isset($info["belong"])) {
    $sql = "DELETE FROM qilian_relationships WHERE content_id={$id}";
    if($db->delete($sql)) {
        foreach($info["belong"] as $value) {
            $sql = "INSERT INTO qilian_relationships(content_id, list_id) VALUES({$id}, {$value})";
            $db->insert($sql);
        }  
    }else {
        exit(Result::jsonReturn(1, "更新失败"));
    }
}

$sql = "UPDATE qilian_contents SET ";
foreach($info as $key=>$value) {
    switch($key) {
        case "belong":
            break;
        case "type":
            $sql = $sql."{$key}={$value},";
            break;
        case "text":
            $sql = $sql."`{$key}`='{$value}',";
            break;
        default:
            $sql = $sql."{$key}='{$value}',";
            break;          
    }
      
}
$sql = Func::removeComma($sql);
$sql = $sql." WHERE id={$id}";
if($db->update($sql)) {
    $sql = "SELECT * FROM qilian_contents WHERE id={$id}";
    $res = array();
    if($res = $db->getOne($sql)) {
        $sql = "SELECT rel.list_id as belong, lis.name as belongname FROM qilian_relationships as rel".
           " LEFT JOIN qilian_lists as lis ON rel.list_id=lis.id WHERE rel.content_id={$id}";
        $bel = $db->getAll($sql);
        $res['belong'] = array();
        $res['belongname'] = array();
        foreach($bel as $val) {
            array_push($res['belong'], $val['belong']);
            array_push($res['belongname'], $val['belongname']);
        }
        $sql = "SELECT * FROM qilian_attachments WHERE content_id={$id}";
        $attachments = $db->getAll($sql);
        foreach($attachments as $key=>$val) {
            $attachments[$key]['title'] = CDN.$val['title'];
        }
        $res["attachments"] = $attachments;
        exit(Result::jsonReturn(0, "更新成功", $res));        
    }else {
        exit(Result::jsonReturn(0, "更新成功", $info));
    }
}else {
    exit(Result::jsonReturn(1, "更新失败"));
}


?>